<!--
	This page shows the multiple matches when google returns more than 1 match of the zipcode or city name user inputs.
-->

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
	
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0;" />
	<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
	
	<link rel="stylesheet" href="/etronix/css/style.css" />
	
	<link href="http://code.google.com/apis/maps/documentation/javascript/examples/default.css" rel="stylesheet" type="text/css" />
	<script type="text/javascript" src="http://code.google.com/apis/gears/gears_init.js"></script> 
	<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true&libraries=geometry"></script>
	
<?php 
echo <<< HERE
<script type="text/javascript"> 
 
var initialLocation;
var browserSupportFlag =  new Boolean();
var map;
  
var myLoc = new Array();
  var message = new Array();
  var info = new Array();
HERE;

$loc = $_REQUEST['location'];	
echo <<< HERE

var address = "$loc"; 

var geocoder;
var lat_temp;
var lng_temp;
HERE;

// initialize() is to show all the matches of the zipcode or city name user inputs.
echo <<< HERE
function initialize() {
	
  // Try W3C Geolocation method (Preferred)
  if(navigator.geolocation) {
    browserSupportFlag = true;
    navigator.geolocation.getCurrentPosition(function(position) {
		if (address == "") {
			window.location = "text.php?location="+address;
		} else {
			zipcode_check();
			
			geocoder = new google.maps.Geocoder();
			geocoder.geocode( { 'address': address}, function(results, status) {
				if (status == google.maps.GeocoderStatus.OK) {
					var cont_h = document.getElementById("Content");
					var l_h = "<div><h2>Multiple matches. Please Choose one</h2></div>";
					var oDiv_h = document.createElement('div');
					oDiv_h.innerHTML = l_h;
					cont_h.appendChild(oDiv_h);
					
					var len = results.length;
					if (len == 1) {
						window.location = "text.php?location="+results[0].formatted_address;
					} else {
						var addr_temp = new Array();
						for (var a = 0; a < len; a++) {
							lat = results[a].geometry.location.lat();
							lng = results[a].geometry.location.lng();
							addr_temp[a] = results[a].formatted_address;
							var cont = document.getElementById("Content");
							var l;
							l = "<div><a href='text.php?location=" + addr_temp[a] + "'>" + addr_temp[a] + "</a></div>";
							var oDiv = document.createElement('div');
							oDiv.innerHTML = l;
							cont.appendChild(oDiv);
						}
					}
				} else {
					alert("Geocode was not successful for the following reason: " + status);
				}
			});
		}
		
    }, function() {
      handleNoGeolocation(browserSupportFlag);
    });
  } else if (google.gears) {
    // Try Google Gears Geolocation
    browserSupportFlag = true;
    var geo = google.gears.factory.create('beta.geolocation');
    geo.getCurrentPosition(function(position) {
      initialLocation = new google.maps.LatLng(position.latitude,position.longitude);
    }, function() {
      handleNoGeolocation(browserSupportFlag);
    });
  } else {
    // Browser doesn't support Geolocation
    browserSupportFlag = false;
    handleNoGeolocation(browserSupportFlag);
  }
  
}
HERE;

// zipcode_check() is to check whether the zipcode is valid or not
echo <<< HERE
function zipcode_check() {
	var addr_length = address.length;
	var is_digits = true;
	for (var s = 0; s < addr_length; s++) {
		if (address.charAt(s).charCodeAt(0) >= 48 && address.charAt(s).charCodeAt(0) <= 57){
		} else {
			is_digits = false;
		}
	}
	if (is_digits == true && addr_length != 5) {
		alert("Wrong Zipcode!");
		window.location = "home.php";
	}							
}
 
function handleNoGeolocation(errorFlag) {
  if (errorFlag == true) {
    initialLocation = newyork;
    contentString = "Error: The Geolocation service failed.";
  } else {
    initialLocation = siberia;
    contentString = "Error: Your browser doesn't support geolocation. Are you in Siberia?";
  }
  map.setCenter(initialLocation);
  infowindow.setContent(contentString);
  infowindow.setPosition(initialLocation);
  infowindow.open(map);
}
</script> 
   


</head>

HERE;
?>

<body onload="initialize()">
	<div id="wrapper">			
		<?php include 'header.php'?>
		
		<div id="Content" >
		
		</div>
		
		<?php include 'footer.php'?>
	</div>
</body>
</html>
